﻿using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// Turn base System 
/// </summary>
public class TurnBaseSystem : NetworkManager
{
    public new static TurnBaseSystem singleton;

    public void Awake()
    {
        singleton = this;
    }

    private bool waitPlayerStart = true;

    public override void OnServerConnect(NetworkConnection conn)
    {
        base.OnServerConnect(conn);
    }

    private int turnIndex;

    public void OnGUI()
    {
        if (NetworkServer.active && waitPlayerStart)
        {
            if (GUILayout.Button("开始游戏"))
            {
                if (NetworkServer.connections.Count > 1)
                {
                    waitPlayerStart = false;

                    StartTurn();
                }
                else
                {
                    Debug.LogError("还不能开始游戏,至少要两个人");
                }
            }
        }
        Debug.Log("NetworkServer.connections.Count" + NetworkServer.connections.Count);
    }

    private void StartTurn()
    {
        NetworkServer.connections[turnIndex].playerControllers[0].gameObject.GetComponent<SimpleBallController>().RpcTurnBegin();
    }

    public void AddTurnIndex()
    {
        turnIndex++;

        if (turnIndex >= NetworkServer.connections.Count)
        {
            turnIndex = 0;
        }

        StartTurn();
    }
}
